<template>
  <div class="todoapp">
    <ToDoHeader @getname="addlist" :arr="list"></ToDoHeader>
    <ToDoMain :arr="showArr" @del="dellist"></ToDoMain>
    <ToDoFooter
      :arr="showArr"
      @changeType="changeFoot"
      @clearDone="clearBtn"
    ></ToDoFooter>
  </div>
</template>

<script>
import ToDoHeader from "./components/ToDoHeader.vue";
import ToDoMain from "./components/ToDoMain.vue";
import ToDoFooter from "./components/ToDoFooter.vue";
export default {
  components: {
    ToDoHeader,
    ToDoMain,
    ToDoFooter,
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem("todoList")) || [],
      cons: "all",
    };
  },
  methods: {
    addlist(thename) {
      this.list.push({
        id:
          this.list.length === 0 ? 100 : this.list[this.list.length - 1].id + 1,
        name: thename,
        isDone: false,
      });
    },
    dellist(index) {
      this.list.splice(index, 1);
    },
    changeFoot(constr) {
      this.cons = constr;
    },
    clearBtn() {
      this.list = this.list.filter((obj) => obj.isDone === false);
    },
  },
  computed: {
    showArr() {
      if (this.cons === "ok") {
        return this.list.filter((obj) => obj.isDone === true);
      } else if (this.cons === "no") {
        return this.list.filter((obj) => obj.isDone === false);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      deep: true,
      handler(newVal) {
        localStorage.setItem("todoList", JSON.stringify(newVal));
      },
    },
  },
};
</script>

<style></style>
